﻿using ConsoleApplication2.Models;
using DHTMLX.Common;
using DHTMLX.Scheduler;
using DHTMLX.Scheduler.Data;
using DHTMLX.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ConsoleApplication2.Controllers
{
    public class LightboxControl : Controller
    {
        DHXScheduler scheduler;

        public ActionResult fghfgh(int id)
        {
            var context = new SampleDataContext();
            var current = context.Events.SingleOrDefault(e => e.id == id);
            return View(current);
        }
        //LightboxControl.cs
        public ActionResult Index() {
      scheduler = new DHXScheduler(this);
      
      var box = scheduler.Lightbox.SetExternalLightbox("MVCFormInLightbox/LightboxControl", 420, 195);
      box.ClassName = "custom_lightbox";// if you want to apply your custom style
      return View(scheduler);
}

        //LightboxControl.cs
        public ActionResult Save(Event changedEvent, FormCollection actionValues)
        {
            var action = new DataAction(actionValues);
            if (action.Type != DataActionTypes.Error)
            {
                return null; // NativeSave(changedEvent, actionValues);//where NativeSave is the default save action for updates which can be performed without using lightbox(e.g. d'n'd)
            }

            //Implementation create/update/delete operations. 
            //Note, we use try-catch block to handle exceptions which may be thrown during operations. In this case, we must assign the error status to a DataAction object
            //in order to mark the appropriate action on the client-side as failed.

            action = new DataAction(DataActionTypes.Update, changedEvent.id, changedEvent.id);
            if (actionValues["actionButton"] != null)
            {
                SampleDataContext data = new SampleDataContext();
                try
                {
                    if (actionValues["actionButton"] == "Save")
                    {
                        if (data.Events.SingleOrDefault(ev => ev.id == action.SourceId) != null)
                        {
                            changedEvent = data.Events.SingleOrDefault(ev => ev.id == action.SourceId);
                            TryUpdateModel(changedEvent);
                        }
                        else
                        {
                            action.Type = DataActionTypes.Insert;
                            data.Events.InsertOnSubmit(changedEvent);
                        }
                    }
                    else if (actionValues["actionButton"] == "Delete")
                    {
                        action.Type = DataActionTypes.Delete;
                        changedEvent = data.Events.SingleOrDefault(ev => ev.id == action.SourceId);
                        data.Events.DeleteOnSubmit(changedEvent);
                    }
                    data.SubmitChanges();
                }

                catch (Exception e)
                {
                    action.Type = DataActionTypes.Error;
                }
            }
            else
            {
                action.Type = DataActionTypes.Error;
            }
            return (new SchedulerFormResponseScript(action, changedEvent));
        }
    }
}
